package com.scs.application.modules.finance.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.scs.application.core.exception.BusinessException;
import com.scs.application.core.mvc.controller.FlowController;
import com.scs.application.core.query.QueryContext;
import com.scs.application.core.query.QueryContextHolder;
import com.scs.application.core.utils.StringUtils;
import com.scs.application.modules.dept.entity.ApplyAuto;
import com.scs.application.modules.finance.dto.BillItemDTO;
import com.scs.application.modules.finance.entity.Bill;
import com.scs.application.modules.finance.mapper.BillMapper;
import com.scs.application.modules.finance.service.BillService;
import com.scs.application.modules.sys.model.PageResult;
import com.scs.application.modules.sys.model.RestResult;
import com.google.common.collect.Maps;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 */
@RestController
@RequestMapping("/finance/bill")
public class BillController extends FlowController<BillService, Bill> {
    @Autowired
    private BillMapper billMapper;

    /**
     * 查询本月科室消耗
     * @param
     * @return
     */
    @GetMapping("/findThisMonthDeptUse")
    public RestResult findThisMonthDeptUse() {
        List<Bill> billList = service.findThisMonthDeptUse();
        return RestResult.ok("查询成功",billList);
    }

    /**
     * 耗材品种消耗TOP10
     */
    @GetMapping("/findThisMonthMaterialUse")
    public RestResult findThisMonthMaterialUse() {
        List<BillItemDTO> billList = service.findThisMonthMaterialUse();
        return RestResult.ok("查询成功",billList);
    }

    /**
     * 一物一码月度消耗趋势
     */
    @GetMapping("/findThisYearOmocUse")
    public RestResult findThisYearOmocUse() {
        List<Bill> billList = service.findThisYearOmocUse();
        return RestResult.ok("查询成功",billList);
    }

    /**
     * 本年耗材月度消耗趋势
     */
    @GetMapping("/findThisYearMaterialUse")
    public RestResult findThisYearMaterialUse() {
        List<Bill> billList = service.findThisYearMaterialUse();
        return RestResult.ok("查询成功",billList);
    }


    /**
     * 高值入账  page
     */
    @RequestMapping("/billHighPage")
    public PageResult billHighPage(@RequestBody Map<String, Object> reqData, HttpServletRequest request) {
        QueryContext queryContext = QueryContextHolder.currentContext();
        Map<String, Object> userParam =null;
        //固定参数
        if(reqData.containsKey("userParam")){
            JSONObject ob = JSON.parseObject(reqData.get("userParam")+"");
            userParam= Maps.newHashMap(ob);
            queryContext.setParams(userParam);
        }
        IPage pageResult = service.billHighPage(queryContext);
        return PageResult.ok("查询成功", pageResult.getRecords(), pageResult.getTotal());
    }

    /**
     * 高值入账  list
     */
    @RequestMapping("/billHighList")
    public RestResult billHighList(@RequestBody Map<String, Object> reqData, HttpServletRequest request) {
        QueryContext queryContext = QueryContextHolder.currentContext();
        Map<String, Object> userParam =null;
        //固定参数
        if(reqData.containsKey("userParam")){
            JSONObject ob = JSON.parseObject(reqData.get("userParam")+"");
            userParam= Maps.newHashMap(ob);
            queryContext.setParams(userParam);
        }
        return RestResult.ok("查询成功", service.billHighList(queryContext));
    }

    /**
     * 高值入账  page
     */
    @RequestMapping("/billLowPage")
    public PageResult billLowPage(@RequestBody Map<String, Object> reqData, HttpServletRequest request) {
        QueryContext queryContext = QueryContextHolder.currentContext();
        Map<String, Object> userParam =null;
        //固定参数
        if(reqData.containsKey("userParam")){
            JSONObject ob = JSON.parseObject(reqData.get("userParam")+"");
            userParam= Maps.newHashMap(ob);
            queryContext.setParams(userParam);
        }
        IPage pageResult = service.billLowPage(queryContext);
        return PageResult.ok("查询成功", pageResult.getRecords(), pageResult.getTotal());
    }

    /**
     * 入账，打印
     * @param itemIds 业务ids
     * @param printType 入账打印类型 high:高值，low:低值
     */
    @RequestMapping("/billPrint")
    public RestResult billPrint(@RequestBody Map<String, String> reqData) {
        String printType = reqData.get("printType");
        String itemIds = reqData.get("itemIds");

        if (!Arrays.asList("high","low").contains(printType)) {
            throw  new BusinessException("打印失败，参数printType缺失或者格式不对");
        }

        if (StringUtils.isBlank(itemIds)) {
            throw  new BusinessException("打印失败，参数ids缺失");
        }
        return service.billPrint(printType,Arrays.asList(itemIds.split(",")), false, null);
    }

    /**
     * 入账，打印、全部
     * @param itemIds 业务ids
     * @param printType 入账打印类型 high:高值，low:低值
     */
    @RequestMapping("/billPrintAll")
    public RestResult billPrintAll(@RequestBody Map<String, String> reqData) {
        String printType = reqData.get("printType");
        return service.billPrint(printType,null, true, reqData.get("queryString"));
    }

    /**
     * 入账，打印 添加明细
     * @param itemIds 业务ids
     * @param printType 入账打印类型 high:高值，low:低值
     */
    @RequestMapping("/billPrintAddDtl")
    public RestResult billPrintAddDtl(@RequestBody Map<String, String> reqData) {
        String printType = reqData.get("printType");
        String itemIds = reqData.get("itemIds");
        String billId = reqData.get("billId");
        if (!Arrays.asList("high","low").contains(printType)) {
            throw  new BusinessException("打印失败，参数printType缺失或者格式不对");
        }

        if (StringUtils.isBlank(itemIds)) {
            throw  new BusinessException("打印失败，参数ids缺失");
        }
        return RestResult.ok(service.billPrintAddDtl(printType,Arrays.asList(itemIds.split(",")),billId));
    }


    /**
     * 入账，取消打印
     * @param itemIds 业务ids
     */
    @RequestMapping("/printCancel")
    public RestResult printCancel(@RequestBody Map<String, String> reqData) {
        String itemIds = reqData.get("itemIds");

        if (StringUtils.isBlank(itemIds)) {
            throw  new BusinessException("打印失败，参数ids缺失");
        }
        String[] itemIdArr = itemIds.split(",");
        return RestResult.ok(service.billPrintCancel(itemIdArr, null));
    }

    //记账科室列表消耗清单列表使用
    @GetMapping("/getDeptList")
    public RestResult<List> getDeptList() {
        QueryContext queryContext = QueryContextHolder.currentContext();
        Map<String, Object> mapParam = queryContext.getParams();

        QueryWrapper queryWrapper = Wrappers.<ApplyAuto>query();
        queryWrapper.select("dept_id as value,dept_name as label");
        queryWrapper.groupBy("dept_id");
        queryWrapper.isNotNull("dept_id");

        List<Map<String, Object>> list = service.listMaps(queryWrapper );
        return RestResult.ok("查询成功", list);
    }

    //记账科室列表高值入账列表使用
    @GetMapping("/getDeptListHigh")
    public RestResult<List> getDeptListHigh() {
        return RestResult.ok("查询成功", billMapper.getDeptListHigh(QueryContextHolder.currentContext().getWrapper()));
    }

    //记账科室列表低值入账列表使用
    @GetMapping("/getDeptListLow")
    public RestResult getDeptListLow() {
        return RestResult.ok("查询成功", billMapper.getDeptListLow(QueryContextHolder.currentContext().getWrapper()));
    }

    //顶部合计 高值入账列表使用
    @GetMapping("/sumTopHigh")
    public RestResult sumTopHigh() {
        return RestResult.ok("查询成功", billMapper.sumTopHigh(QueryContextHolder.currentContext().getWrapper()));
    }

    //顶部合计 低值入账列表使用
    @GetMapping("/sumTopLow")
    public RestResult sumTopLow() {
        return RestResult.ok("查询成功", billMapper.sumTopLow(QueryContextHolder.currentContext().getWrapper()));
    }

}
